#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'gtlions'
import psycopg2
import csv
import os
import datetime
import sys

db1 = psycopg2.connect(dbname="bigdatagp", user="gpadmin", host="10.46.219.48")
db2 = psycopg2.connect(dbname="gtlions", user="gpadmin", host="192.168.56.50")
curdb11 = db1.cursor()
curdb21 = db2.cursor()

f = open("gp.csv", "w")
writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)

curdb21.execute("select max(sum_date) from ft_mid_gn_keyword_daily")
max_date = curdb21.fetchone()[0]

print datetime.datetime.now(), "Begin query from source database."

curdb11.execute("select msisdn,lac,cellci,keyword,sum_date from gpmiddle.ft_mid_gn_keyword_daily where sum_date>%s",
                (max_date,))
# curdb11.execute( "select * from gpmg.manager_table where id=%s", ( 4, ) )
print datetime.datetime.now(), "Finsh query from source database."

i = 0
print datetime.datetime.now(), "Begin wirte record into local file."

for row in curdb11:
    # i = i+1
    writer.writerow(row)
f.close()
print datetime.datetime.now(), "Finsh wirte record into local file"
db1.commit()
curdb11.close()

reader = csv.reader(open("gp.csv", "r"))

lines = []
commit = 3000
i = 0
print datetime.datetime.now(), "Begin insert record into target database."
for line in reader:

    lines.append(line)
    if len(lines) == commit:
        i = i + 1
        current_commit = commit * i
        curdb21.executemany("insert into ft_mid_gn_keyword_daily values(%s,%s,%s,%s,%s)", lines)
        db2.commit()
        print datetime.datetime.now(), "------ insert " + str(current_commit).rjust(8) + " record into target database."
        # del(lines)
        lines = []
curdb21.executemany("insert into test values(%s,%s,%s,%s,%s)", lines)
db2.commit()
db2.close()
print datetime.datetime.now(), "------ insert " + str(current_commit + len(lines)).rjust(
    8) + " record into target database."
print datetime.datetime.now(), "Finsh insert record into target database."
